package test;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;
import javax.swing.table.TableCellEditor;

public class T {
	public static void main(String[] args) {
		final JPopupMenu popup = new JPopupMenu();
		popup.add(new JMenuItem("MenuItem-1 "));
		popup.add(new JMenuItem("MenuItem-2 "));
		popup.add(new JMenuItem("MenuItem-3 "));

		final MouseListener ml = new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					Component c = e.getComponent();
					popup.show(c, e.getX(), e.getY());
				}
			}
		};

		JTable table = new JTable(5, 5) {
			public Component prepareEditor(TableCellEditor editor, int row,
					int column) {
				Component edComp = super.prepareEditor(editor, row, column);
				edComp.removeMouseListener(ml);
				edComp.addMouseListener(ml);

				return edComp;
			}
		};
		JScrollPane sp = new JScrollPane(table);

		table.addMouseListener(ml);

		JFrame f = new JFrame();
		f.getContentPane().add(sp, BorderLayout.CENTER);
		f.pack();
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
}
